﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.TcCommands.CApAWCSearchMFGEnabler
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using Tecnomatix.Engineering;
using UiInternalControls;


namespace DnProcessSimulateCommands.TcCommands;

internal class CApAWCSearchMFGEnabler : ITxCommandEnabler
{
  private bool _isRobcadStudyMode;
  private bool _isDataLoaded;

  public CApAWCSearchMFGEnabler()
  {
    this.RegisterToEvents();
    this.updateStates();
  }

  public bool Enable => this._isRobcadStudyMode && !TxTcStudyReservation.IsReadOnly();

  private void OnStudyModeChanged(
    object sender,
    TxDocumentCollection_StudyModeChangedEventArgs args)
  {
    this.CheckStudyMode();
  }

  private void OnDocumentLoaded(object sender, TxDocumentCollection_DocumentLoadedEventArgs args)
  {
    this.updateStates();
  }

  private void OnActiveDocumentUnloading(object sender, TxDocument_UnloadingEventArgs args)
  {
    this._isDataLoaded = false;
    this._isRobcadStudyMode = false;
  }

  private void CheckStudyMode()
  {
    if (!this._isDataLoaded)
      return;
    ITxPlatformGlobalServicesProvider servicesProvider = TxApplication.ActiveDocument.PlatformGlobalServicesProvider;
    if (servicesProvider == null)
      return;
    this._isRobcadStudyMode = !servicesProvider.IsLineSimulationMode;
  }

  internal void RegisterToEvents()
  {
    TxApplication.DocumentCollection.DocumentLoaded += new TxDocumentCollection_DocumentLoadedEventHandler(this.OnDocumentLoaded);
    TxApplication.ActiveDocument.Unloading += new TxDocument_UnloadingEventHandler(this.OnActiveDocumentUnloading);
    TxApplication.DocumentCollection.StudyModeChanged += new TxDocumentCollection_StudyModeChangedEventHandler(this.OnStudyModeChanged);
  }

  internal void UnregisterFromEvents()
  {
    TxApplication.DocumentCollection.DocumentLoaded -= new TxDocumentCollection_DocumentLoadedEventHandler(this.OnDocumentLoaded);
    TxApplication.ActiveDocument.Unloading -= new TxDocument_UnloadingEventHandler(this.OnActiveDocumentUnloading);
    TxApplication.DocumentCollection.StudyModeChanged -= new TxDocumentCollection_StudyModeChangedEventHandler(this.OnStudyModeChanged);
  }

  internal void updateStates()
  {
    ITxPlatformGlobalServicesProvider servicesProvider = TxApplication.ActiveDocument.PlatformGlobalServicesProvider;
    this._isDataLoaded = servicesProvider != null && servicesProvider.IsDataLoaded();
    this.CheckStudyMode();
  }
}
